from PyQt5.QtWidgets import (QTableWidget,
                             QTableWidgetItem, QWidget, QPushButton, QVBoxLayout, QDialog, QFormLayout, QLineEdit,
                             QHBoxLayout)
from src.forward.base.RightWidget import RightWidget
from src.forward.base.bean import forward, Table_ABC


@forward(name='学生情况')
class Student(RightWidget):
    def __init__(self):
        super().__init__()
        self.layout=QHBoxLayout()
        self.setLayout(self.layout)
        #分2列
        self.left=QWidget()
        self.left.setFixedWidth(200)
        self.left_out=QVBoxLayout()
        self.left.setLayout(self.left_out)
        self.addB=QPushButton('增加')
        self.addB.clicked.connect(self.add)
        self.left_out.addWidget(self.addB)

        self.student_table=Table_ABC()
        self.student_table.setStyleSheet("""
            QTableWidget { background-color: #DDF5CB; }
            QTableWidget::item:selected { background-color: #FF0000; color: white; }  # 例如：选中时背景为红色，文字为白色
        """)
        self.left_out.addWidget(self.student_table)
        self.right=QWidget()
        right_layout=QVBoxLayout()
        self.right.setLayout(right_layout)

        self.right_up=QWidget()
        self.right_up.setStyleSheet("""
                    QWidget { background-color: #DDF5CB;
                     border: 2px solid black;
                     }
                """)
        self.right_down=QTableWidget()
        self.right_down.setStyleSheet("""
                            QTableWidget { background-color: #CBE2F5;   
                             }
                        """)
        right_layout.addWidget(self.right_down)
        right_layout.addWidget(self.right_up)
        self.layout.addWidget(self.left,1)
        self.layout.addWidget(self.right,4)

    def get_data(self):
        datas=[]
        from src.backend.beanfactory import BeanFactory
        student = BeanFactory.getCursor().query('select * from student')
        datas.append(student)
        return datas

    def fill_data(self):
        datas=self.get_data()
        self.student_table.fill_data(datas[0],['发布'])

    def add(self):
        # 创建并显示新的对话框
        from src.forward.task import AddDialog
        dialog = AddDialog(self)
        result = dialog.exec_()
        if result == QDialog.DialogCode.Accepted:
            self.fill_data()
        elif result == QDialog.DialogCode.Rejected:
            print("对话框通过点击取消按钮关闭")
        else:
            print("对话框通过其他方式关闭")




